package com.open.pokergames.texas.repository.viewModel

import com.open.pokergames.NetConfig
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.comm.repository.entity.UserEntity
import com.open.pokergames.texas.repository.TexasApi
import com.open.pokergames.texas.repository.entity.RoomEntity
import com.open.pokergames.texas.repository.entity.TexasVo
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class TexasViewModel {

    fun usersPage(pageReq: PageReq<TexasVo>) {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())
            .create(TexasApi::class.java)
            .usersPage(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<UserEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<UserEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "usersPage"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<UserEntity>>) {
                    result.eventType = "usersPage"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun roomPage(pageReq: PageReq<TexasVo>) {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())
            .create(TexasApi::class.java)
            .roomPage(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<RoomEntity>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<RoomEntity>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "roomPage"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<RoomEntity>>) {
                    result.eventType = "roomPage"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun roomAdd(room: RoomEntity) {
        RxRetrofit.init(NetConfig.BASE_URL, NetConfig.getHeader())
            .create(TexasApi::class.java)
            .roomAdd(room)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<RoomEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<RoomEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "roomAdd"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<RoomEntity>) {
                    result.eventType = "roomAdd"
                    EventBus.getDefault().post(result)
                }
            })
    }
}